package com.exp.exp03.service;

import com.exp.exp03.pojo.RegionStats;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
public class MyService implements InitializingBean {
    private List<RegionStats> statList;

    @Scheduled(cron = "0 0 1 * * *")
    public void setStatList() throws IOException {
        String uriTemplate = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        URI uri = UriComponentsBuilder.fromUriString(uriTemplate).build(42);
        RequestEntity<Void> requestEntity = RequestEntity.get(uri)
                .headers(httpHeaders -> httpHeaders.add("User-Agent", "Chrome/81.0.4044.138"))
                .build();
        ResponseEntity<Resource> exchange = new RestTemplate().exchange(requestEntity, Resource.class);
        Resource body = exchange.getBody();
        assert body != null;
        Reader in = new InputStreamReader(body.getInputStream());
        Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
        List<RegionStats> tempList = new ArrayList<>();
        int a = 0;
        for (CSVRecord record : records) {
            if (a == 0) {
                a = 1;
                continue;
            }
            RegionStats r = new RegionStats();
            r.setProvince_state(record.get(0));
            r.setCountry_region(record.get(1));
            r.setLatestTotalCases(Integer.parseInt(record.get(record.size() - 1)));
            r.setDiffFromPrevDay(Integer.parseInt(record.get(record.size() - 1)) - Integer.parseInt(record.get(record.size() - 2)));
            tempList.add(r);
        }
        this.statList = new ArrayList<>();
        this.statList.addAll(tempList);
    }

    public List<RegionStats> getStatList() {
        return this.statList;
    }

    public List<String> getCountryOrRegionList(List<RegionStats> statList) {
        List<String> listTemp = new ArrayList<>();
        for (RegionStats item : statList) {
            listTemp.add(item.getCountry_region());
        }
        List<String> resultList = new ArrayList<>();
        for (String s : listTemp) {
            if (!resultList.contains(s)) {
                resultList.add(s);
            }
        }
        return resultList;
    }

    public List<RegionStats> getStatListByCountryOrRegion(String countryOrRegion, List<RegionStats> statList) {
        List<RegionStats> resultList = new ArrayList<>();
        for (RegionStats item : statList) {
            if (item.getCountry_region().equals(countryOrRegion)) {
                resultList.add(item);
            }
        }
        return resultList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.setStatList();
    }
}

